/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pdlv2;
import graph.*;
import query.*;
import java.io.*;


/**
 *
 * @author Gonzalo
 */
public class Main {
    static final int PUERTO=69;
    static PDLv2 db;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        openDB();
        //new Performance();
        consola();
        //generar();
        closeDB();
    }

    public static void generar(){
        long num=db.numNodes();
        /*for(long node=438000;node<=num;node++){
            db.setAttribute(node,"name","persona"+(node%10));
            db.setAttribute(node,"age",Math.round(Math.random()*100+10));
            if(node%1000==0)
                db.debug("name age "+node);
        }
        db.flush();
        for(long node=1;node<=num;node++){
            db.addEdge("parent", Math.round(Math.random()*num), node);
            db.addEdge("parent", Math.round(Math.random()*num), node);
            if(node%1000==0)
                db.debug("parent "+node);
        }
        db.flush();
*/
        for(long node=1;node<=num;node++){
            for(int k=1;k<=120;k++)
                db.newEdge("friend", node,node+Math.round(Math.random()*5000));
            if(node%1000==0)
                db.debug("friend "+node);
        }
        db.flush();
    }

    public static void consola(){

        //Escuchando el teclado
        BufferedReader lectura = new BufferedReader (new InputStreamReader(System.in));
        String consulta="";
        Nodes resultado;
        Query Q;
        DDL ddl;
        DML dml;
        Debug deg;
        Parser parser=new Parser();
        
        while(true){
                resultado=null;
                try{
                    System.out.print("PDL> ");
                    consulta = lectura.readLine().trim();
                    if(lectura.ready())
                        consulta = consulta+lectura.readLine().trim();
                }catch(Exception e){
                    db.debug(e);
                }
                if(consulta.compareTo("exit")==0)
                    break;
                if(consulta.compareTo("")==0) continue;
                if(parser.isDebug(consulta)){
                    deg=parser.getDebug(consulta);
                    db.speed("");
                    db.ejecutarDebug(deg);
                    db.speed("Debug: "+deg.print());
                }else if(parser.isDDL(consulta)){
                    ddl=parser.getDDL(consulta);
                    db.speed("");
                    db.ejecutarDLL(ddl);
                    db.speed("DDL: "+ddl.print());
                }else if(parser.isDML(consulta)){
                    dml=parser.getDML(consulta);
                    db.speed("");
                    resultado=db.ejecutarDML(dml);
                    db.print(resultado);
                    db.debug("********************************************");
                    db.speed("DML: "+dml.print()+" #"+resultado.count());
                }else{
                    Q=parser.getQuery(consulta);
                    db.speed("");
                    resultado=db.ejecutarQuery(Q);
                    db.speed("Query: "+Q.print()+" #"+resultado.count());
                    db.debug("********************************************");
                    db.print(resultado);
                }
        }

    }

    public static void openDB(){
        db=new PDLv2("D:/PDLv2");
        db.speed();
        db.debug("********************************************");
    }
    public static void closeDB(){
        db.close();
    }
}
